package cn.zldemo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.*;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;
import cn.zldemo.util.ViewMapping;
import cn.zldemo.util.ViewMappingUtil;

@ViewMapping(id = R.layout.activity_flipper)
public class FlipperActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
	private GestureDetector mGestureDetector;

	@ViewMapping(id = R.id.viewFlipper)
	private ViewFlipper mViewFlipper;
	@ViewMapping(id = R.id.change1)
	private Button mChangeBtn;
	public static Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewMappingUtil.mapView(this, this);
		mGestureDetector = new GestureDetector(this, this);
		mChangeBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// mViewFlipper.setFlipInterval(3000);
				// mViewFlipper.startFlipping();
			}
		});
		mContext = this;
		Box box = new Box();
		Cat schrodingerCat = new Cat();
		box.hiddenCat = schrodingerCat;
		Docker.container = box;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_flipper, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		// noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.mGestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		if (e1.getX() > e2.getX()) {
			Toast.makeText(this, "next", Toast.LENGTH_SHORT).show();
			if (distanceX > 20) {
				mViewFlipper.showNext();
			}

		} else {
			Toast.makeText(this, "pre", Toast.LENGTH_SHORT).show();
			if (distanceX < -20) {
				mViewFlipper.showPrevious();
			}
		}
		return true;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return false;
	}
}

class Cat {
}

class Box {
	Cat hiddenCat;
}

class Docker {
	static Box container;
}